package fr.univ.lille1.android.tests;

import java.util.ArrayList;
import java.util.List;

import android.support.v4.app.Fragment;
import android.test.ActivityInstrumentationTestCase2;
import android.widget.ListView;
import fr.univ.lille1.android.CommentActivity_;
import fr.univ.lille1.android.R;
import fr.univ.lille1.android.adapters.CommentAdapter_;
import fr.univ.lille1.android.domain.Message;
import fr.univ.lille1.android.fragments.ListCommentsFragment_;

public class CommentActivityTest extends
		ActivityInstrumentationTestCase2<CommentActivity_> {
	private static final String TAG_LIST_COMMENT = "ListCommentsFragment";
	private CommentActivity_ mActivity;
	private ListCommentsFragment_ mFragment;

	public CommentActivityTest() {
		super(CommentActivity_.class);
	}

	@Override
	protected void setUp() throws Exception {
		super.setUp();
		mActivity = getActivity();
		mFragment = (ListCommentsFragment_) startFragment(new ListCommentsFragment_());
	}

	private Fragment startFragment(final Fragment fragment) {
		mActivity.getSupportFragmentManager() //
				.beginTransaction() //
				.add(R.id.frameLayoutComments, fragment, TAG_LIST_COMMENT) //
				.commit();
		getInstrumentation().waitForIdleSync();
		return mActivity.getSupportFragmentManager().findFragmentByTag(
				TAG_LIST_COMMENT);
	}

	public void testAdapterListComments() {
		final List<Message> comments = new ArrayList<Message>();
		comments.add(new Message(0, "", "", 0, 0, null, null));
		comments.add(new Message(0, "", "", 0, 0, null, null));
		final ListView list = mFragment.getListView();
		final CommentAdapter_ adapter = (CommentAdapter_) list.getAdapter();
		adapter.addAll(comments);
		assertEquals(comments.size(), adapter.getCount());
		mActivity.finish();
	}
}
